/**
 * @author hhlxy
 * @version 1.0
 * @since 2025/8/14
 */
package cn.yilongquan.Listener;

import cn.hutool.core.util.ClassUtil;
import cn.yilongquan.authorize.PreAuthorize;
import cn.yilongquan.mapper.PermissionMapper;
import cn.yilongquan.system.domain.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Set;

@Component
public class PermissionScan {
    @Autowired
    private PermissionMapper permissionMapper;

    private static final String PKG_PREFIX = "cn.yilongquan.controller";

    public void scanPermission() {
        //怎么去找到对应的类 才能找到方法
        //cn.yuxiayuan.controller.*

        String path = this.getClass().getResource("/").getPath() + "/cn/yilongquan/controller";
        File file = new File(path);
        //得到所有的controller类
        File[] files = file.listFiles();
        //确保是controller
        Set<Class<?>> classes = ClassUtil.scanPackageByAnnotation(PKG_PREFIX, RestController.class);
        for (Class<?> aClass : classes) {
            //反射  得到你类里面的所有方法
            Method[] methods = aClass.getMethods();
            if (methods == null || methods.length < 1) {//排除当前这个类是空的没有方法
                return;
            }
            //循环你的方法
            for (Method method : methods) {
                // 1.得到该方法的请求地址
                String uri = getUri(aClass, method);//url

                PreAuthorize preAuthorize = method.getAnnotation(PreAuthorize.class);
                if (preAuthorize == null) {
                    continue;
                }
                String descs = preAuthorize.descs();
                String sn = preAuthorize.sn();
                String name = preAuthorize.name();
                Permission permission = new Permission();
                permission.setDescs(descs);
                permission.setName(name);
                permission.setSn(sn);
                permission.setUrl(uri);
                //数据库里面没有才添加
                Permission sn1 = permissionMapper.findSn(sn);
                if (sn1 == null) {
                    permissionMapper.add(permission);
                } else {
                    permission.setId(sn1.getId());
                    permissionMapper.update(permission);
                }
            }
        }

    }

    /**
     * @param clazz:  类字节码对象
     * @param method: 方法对象
     * @Description: 根据类的字节码和类的方法得到方法的请求地址
     * @return: java.lang.String
     **/
    private String getUri(Class clazz, Method method) {
        String classPath = "";
        Annotation annotation = clazz.getAnnotation(RequestMapping.class);
        if (annotation != null) {
            RequestMapping requestMapping = (RequestMapping) annotation;
            String[] values = requestMapping.value();
            if (values != null && values.length > 0) {
                classPath = values[0];
                if (!"".equals(classPath) && !classPath.startsWith("/")) {
                    classPath = "/" + classPath;
                }
            }
        }
        GetMapping getMapping = method.getAnnotation(GetMapping.class);
        String methodPath = "";
        if (getMapping != null) {
            String[] values = getMapping.value();
            if (values != null && values.length > 0) {
                methodPath = values[0];
                if (!"".equals(methodPath) && !methodPath.startsWith("/")) {
                    methodPath = "/" + methodPath;
                }
            }
        }
        PostMapping postMapping = method.getAnnotation(PostMapping.class);
        if (postMapping != null) {
            String[] values = postMapping.value();
            if (values != null && values.length > 0) {
                methodPath = values[0];
                if (!"".equals(methodPath) && !methodPath.startsWith("/")) {
                    methodPath = "/" + methodPath;
                }
            }
        }
        DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
        if (deleteMapping != null) {
            String[] values = deleteMapping.value();
            if (values != null && values.length > 0) {
                methodPath = values[0];
                if (!"".equals(methodPath) && !methodPath.startsWith("/")) {
                    methodPath = "/" + methodPath;
                }
            }
        }
        PutMapping putMapping = method.getAnnotation(PutMapping.class);
        if (putMapping != null) {
            String[] values = putMapping.value();
            if (values != null && values.length > 0) {
                methodPath = values[0];
                if (!"".equals(methodPath) && !methodPath.startsWith("/")) {
                    methodPath = "/" + methodPath;
                }
            }

        }
        PatchMapping patchMapping = method.getAnnotation(PatchMapping.class);
        if (patchMapping != null) {
            String[] values = patchMapping.value();
            if (values != null && values.length > 0) {
                methodPath = values[0];
                if (!"".equals(methodPath) && !methodPath.startsWith("/")) {
                    methodPath = "/" + methodPath;
                }
            }
        }
        RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
        if (requestMapping != null) {
            String[] values = requestMapping.value();
            if (values != null && values.length > 0) {
                methodPath = values[0];
                if (!"".equals(methodPath) && !methodPath.startsWith("/")) {
                    methodPath = "/" + methodPath;
                }
            }
        }
        return classPath + methodPath;
    }
}
